package core;

import java.util.ArrayList;
import java.util.Timer;

import exception.ServerDownException;
import values.Topic;

public class Topics 
{
	private static Timer timer;
	
	private static Topics_Task task;
	
	ArrayList<Topic> topics;
	
	public Topics() throws ServerDownException
	{
		
		timer= new Timer();
		task= new Topics_Task();
		topics= Core.getTopics();
		timer.scheduleAtFixedRate(task, System.currentTimeMillis(), 1000*60);
	}
	
	
	public void setTopics(ArrayList<Topic> topics)
	{
		this.topics=topics;
	}
	
	public ArrayList<Topic> getTopics()
	{
		return topics;
	}
	
	public Topic getMostCommentedTopic(String start)
	{
		int position=-1;
		
		int counter=0;
		
		for(int i=0;i<topics.size();i++)
		{
			Topic topic=topics.get(i);
			String name=topic.getTopic_name();
			int mentions=topic.getCounter();
			
			if(name.startsWith(start) && mentions>counter)
			{
				position=i;
				counter=mentions;
			}
		}
		if(position>-1)
		{
			return topics.get(position);
		}
		else
			return null;
	}
	
}
